<template>
  <div class="searchBox">
    <div class="row">
      <div class="left">
        <el-icon><Search /></el-icon>
        <span>筛选搜索</span>
      </div>
      <div class="right">
        <el-button @click="queryParams.keyword = ''">重置</el-button>
        <el-button type="primary" @click="getRoleList">查询搜索</el-button>
      </div>
    </div>
    <div class="row">
      <span>账号/姓名:</span>
      <el-input
        v-model="queryParams.keyword"
        placeholder="请输入姓名、账号搜索"
      ></el-input>
    </div>
  </div>
  <div class="addArea">
    <el-button
      type="primary"
      @click="showDialog = true"
      v-permission="'addRole'"
      >添加</el-button
    >
  </div>
  <el-table
    :data="userList"
    border
    table-layout="auto"
    cell-class-name="align-c"
  >
    <el-table-column label="编号" prop="id" />
    <el-table-column label="角色名称" prop="name" />
    <el-table-column label="描述" prop="description" />
    <el-table-column label="用户数" prop="sort" />
    <el-table-column label="添加时间" prop="createTime">
      <template #default="{ row }">
        {{ formateDate(row.createTime) }}
      </template>
    </el-table-column>
    <el-table-column label="是否启用">
      <template #default="{ row }">
        <el-switch
          v-model="row.status"
          :active-value="1"
          :inactive-value="0"
          @change="switchStatus(row)"
        ></el-switch>
      </template>
    </el-table-column>
    <el-table-column label="操作" width="180">
      <template #default="{ row }">
        <el-button
          text
          type="primary"
          v-permission="'allocMenu'"
          @click="allocMenu(row.id)"
          >分配菜单</el-button
        >
        <el-button
          text
          type="primary"
          v-permission="'allocResource'"
          @click="router.push('/ums/allocResource?id=' + row.id)"
          >分配资源</el-button
        >
        <el-button
          text
          type="primary"
          @click="editRole(row.id)"
          v-permission="'editRole'"
          >编辑</el-button
        >
      </template>
    </el-table-column>
  </el-table>
  <ElConfigProvider :locale="zhCn">
    <div class="page">
      <el-pagination
        v-model:current-page="queryParams.pageNum"
        v-model:page-size="queryParams.pageSize"
        :page-sizes="[10, 20, 50, 100]"
        background
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        @size-change="getRoleList"
        @current-change="getRoleList"
      />
    </div>
  </ElConfigProvider>

  <EditRole :show="showDialog" @hide="closeDialog" :id="editId"></EditRole>
</template>
<script setup lang="ts">
import { ref, reactive } from "vue";
import { changeRoleStatusApi, getRoleByUserNameApi } from "./api";
import { Search } from "@element-plus/icons-vue";
import { ElConfigProvider, ElMessage } from "element-plus";
import zhCn from "element-plus/lib/locale/lang/zh-cn";
import { formateDate } from "@/utils";
import EditRole from "./editRole.vue";
import { useRouter } from "vue-router";

const queryParams = reactive<IApiParams>({
  keyword: "test",
  pageNum: 1,
  pageSize: 10,
});
const userList = ref<List[]>([]);
const total = ref(0);
const showDialog = ref(false);
const editId = ref(0);
const showRole = ref(false);
const allocId = ref(0);
const router = useRouter();

const editRole = (id: number) => {
  editId.value = id;
  showDialog.value = true;
};
//分配菜单
const allocMenu = (id: number) => {
  router.push("/ums/allocMenu?id=" + id);
};
//关闭添加框
const closeDialog = (isReload?: Boolean) => {
  showDialog.value = false;
  if (isReload) getRoleList();
  editId.value = 0;
};
const getRoleList = () => {
  getRoleByUserNameApi(queryParams).then((res) => {
    if (res.code === 200) {
      userList.value = res.data.list;
      total.value = res.data.total;
    }
  });
};

getRoleList();

const switchStatus = (row: List) => {
  changeRoleStatusApi(row.id as number, row.status)
    .then((res) => {
      if (res.code === 200) {
        ElMessage.success("修改成功");
      } else {
        //修改失败了要把状态改回去
        row.status = row.status === 1 ? 1 : 0;
      }
    })
    .catch(() => {
      //修改失败了要把状态改回去
      row.status = row.status === 1 ? 1 : 0;
    });
};
</script>
<style lang="less" scoped>
:deep(.align-c) {
  .cell {
    text-align: center;
  }
}
.searchBox {
  box-shadow: 0 0 4px 2px #ccc;
  border-radius: 10px;
  padding: 20px;
  margin-bottom: 30px;
  .row {
    padding: 10px 0;
    display: flex;
    &:first-child {
      justify-content: space-between;
    }
    &:last-child {
      width: 400px;
      span {
        width: 100px;
      }
    }
  }
}

.page {
  display: flex;
  flex-direction: row-reverse;
  :deep(.el-pagination) {
    margin-top: 20px;
  }
}
</style>
